#!/usr/bin/python3

import threading
import time

from SearchAndFenci import SearchAndFenci

class fenciThread (threading.Thread):
    def __init__(self, threadID, name, flag, SeedKeyword, searchLog,mutex):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.flag = flag
        self.SeedKeyword=SeedKeyword
        self.searchLog = searchLog
        self.mutex = mutex
    def run(self):
        print ("开始线程：" + self.name + "并处理---" + self.SeedKeyword + "---关键字")

        searchAndFenci = SearchAndFenci()
        for index, i in enumerate(self.searchLog):
            if (index % 10000 == 0):
                if (index == 0):
                    start_time = time.time()
                end_time = time.time()
                print("处理种子关键字一万条数据时间开销为{:.4f}秒".format(end_time - start_time))
                start_time = time.time()
            temp = i.split()
            # temp中保存了文件中的一行按1照空格进行分割后得到的字符串列表，下面需要遍历列表中每个字符串 找到与我们输入的种子关键字匹配的字符串并输出
            for j in temp:
                j = j.decode('UTF-8')
                #一个一个遍历关键字,i是下标,k是种子关键字
                if self.SeedKeyword in j:
                    if int(self.flag) == 1:
                        searchAndFenci.jiebafenci(j, self.SeedKeyword)
                    #elif int(flag) == 2:
                    #    pkusegfenci(j, self.SeedKeyword)
                    elif int(self.flag) == 3:
                        searchAndFenci.thulacfenci(j, self.SeedKeyword)
                    # elif int(flag) == 4:
                    #    pyhanlpfenci(j, self.SeedKeyword)
                    else:
                        searchAndFenci.jiebafenci(j, self.SeedKeyword)

        print("已经找到所有与种子关键字---"+self.SeedKeyword+"---有关的搜索记录，下面开始把结果写入文件并退出线程：" + self.name)
        #创建文件夹
        searchAndFenci.creatDir(self.SeedKeyword)
        #将搜索结果保存进文件夹
        searchAndFenci.outputResult(self.SeedKeyword)
        #将分词结果进行保存进文件夹
        searchAndFenci.outputFenci(self.SeedKeyword)
        # 将停词分词结果进行保存进文件夹
        searchAndFenci.outputFenciwithStopDic(self.SeedKeyword)
        #将中介关键词排序并存入进文件夹
        searchAndFenci.outputMidKeyWithStopDic(self.SeedKeyword)
        # 上锁锁定
        self.mutex.acquire()
        #追加搜索记录
        searchAndFenci.appendSearchRecords(self.SeedKeyword)
        #进行
        #这里是解锁
        self.mutex.release()





